#!/usr/bin/env python3.0

import ossaudiodev
import sys
import madsimple

AUDIODEV = "/dev/dsp"

if len(sys.argv)<2:
	print("{0:s} mp3file".format(sys.argv[0]))
	sys.exit(1)

if sys.argv[1]=='-debug':
	del sys.argv[1]
	import pdb
	pdb.set_trace()


def output(fo, pcm_data):
	fo.write(pcm_data)
	return madsimple.FLOW_CONTINUE

def error(fo, data):
	pass
def h(fo, a):
	return madsimple.FLOW_CONTINUE

def decode_start():
	dev = ossaudiodev.open(AUDIODEV, "w")
	dev.setfmt(ossaudiodev.AFMT_S16_LE)
	dev.channels(2)
	dev.speed(44100)

	with open(sys.argv[1], "br") as mp3:
		decoder = madsimple.Decoder(mp3, dev, 5000)
		decoder.run(header=h, output=output, error=error)
		print("finished!")

def play_pcm():
	dev = ossaudiodev.open(AUDIODEV, "w")
	dev.setfmt(ossaudiodev.AFMT_S16_LE)
	dev.channels(2)
	dev.speed(44100)

	with open(sys.argv[1], "br") as pcm:
		while True:
			b = pcm.read(2048)
			if b==None:
				break
			dev.write(b)

#decode_start()
play_pcm()

print("exit")



